package com.lg.my.netty.codec;

import com.lg.netty.codec.ProtostuffUtil;
import com.lg.netty.codec.User;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author by Mr. Li 2021/4/22 2:20
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 获取到参数，进行编码
        //System.out.println("client coder is string ,msg is :" + msg.toString());
        //System.out.println("从客户端读取到Object：" + ((User)msg).toString());

        // 自定义 使用protocol 的协议 进行编解码
        //ByteBuf buf = (ByteBuf) msg;
        //byte[] bytes = new byte[buf.readableBytes()];
        //buf.readBytes(bytes);
        //System.out.println("从客户端读取到Object：" + ProtostuffUtil.deserializer(bytes, User.class));

        // 使用自定的编解码器
        System.out.println("从客户端读取到Object：" + msg);

        //ctx.channel().writeAndFlush("返回数据" + msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
